package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.qztech.sdk.R;


public class IndexerView extends View {

    private int mIndexCount;
    private int mCurrentIndex;
    private int mCurrentColor;
    private int mColor;
    private int linePadding;

    private float maxPercent = 0.8f;

    private Paint mPaint;
    private Context mContext;
    private float mRadius;

    private Rect mRect = new Rect();
    private int padding;

    public IndexerView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public IndexerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initView();
    }

    protected void initView() {
        final Resources res = mContext.getResources();
        padding = res.getDimensionPixelSize(com.hanlink.res.R.dimen.padding_tiny);
        setPadding(padding, padding, padding, padding);
        mColor = res.getColor(com.hanlink.res.R.color.index_color);
        mCurrentColor = res.getColor(com.hanlink.res.R.color.index_current_color);
        mCurrentIndex = 0;
        mIndexCount = 0;
        mRadius = res.getDimensionPixelSize(com.hanlink.res.R.dimen.indexer_radius);
        linePadding = res.getDimensionPixelSize(com.hanlink.res.R.dimen.indexer_padding);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.getClipBounds(mRect);
        float width = (linePadding + mRadius) * mIndexCount - linePadding;
        int maxWidth = (int) (getMeasuredWidth() * maxPercent);
        if (width > maxWidth) {
            width = maxWidth;
        }
        float cx = (mRect.right - mRect.left - width) / 2;
        float cy = (mRect.bottom - mRect.top) / 2;
        for (int i = 0; i < mIndexCount; i++) {
            if (i != mCurrentIndex) {
                mPaint.setColor(mColor);
            } else {
                mPaint.setColor(mCurrentColor);
            }
            canvas.drawCircle(cx, cy, mRadius / 2, mPaint);
            cx += mRadius + linePadding;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mRadius > (getMeasuredHeight() - getPaddingTop() - getPaddingBottom())) {
            mRadius = getMeasuredHeight();
            postInvalidate();
        }
    }

    public int getIndexCount() {
        return mIndexCount;
    }

    public void setIndexCount(int mIndexCount) {
        this.mIndexCount = mIndexCount;
        postInvalidate();
        requestLayout();
    }

    public int getCurrentIndex() {
        return mCurrentIndex;
    }

    public void setCurrentIndex(int index) {
        if (index < mIndexCount && index >= 0) {
            this.mCurrentIndex = index;
            postInvalidate();
        }
    }

    public int getCurrentColor() {
        return mCurrentColor;
    }

    public void setCurrentColor(int mCurrentColor) {
        this.mCurrentColor = mCurrentColor;
        postInvalidate();
    }

    public int getColor() {
        return mColor;
    }

    public void setColor(int mColor) {
        this.mColor = mColor;
        postInvalidate();
    }

    public int getLinePadding() {
        return linePadding;
    }

    public void setLinePadding(int linePadding) {
        this.linePadding = linePadding;
        postInvalidate();
    }

    public void addNewIndex() {
        mIndexCount++;
        postInvalidate();
        requestLayout();
    }
}
